package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.domain.TravelContent;
import cn.wolfcode.luowowo.article.mapper.TravelContentMapper;
import cn.wolfcode.luowowo.article.mapper.TravelMapper;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.common.util.KeysUtil;
import cn.wolfcode.luowowo.service.ITravelStatisRedisService;
import cn.wolfcode.luowowo.vo.StatisVO;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;


@Service
public class TravelServiceImpl implements ITravelService {

    @Autowired
    private TravelMapper travelMapper;

    @Autowired
    private TravelContentMapper travelContentMapper;

    @Reference
    private ITravelStatisRedisService statisRedisService;


    @Override
    public PageInfo<Travel> queryForList(TravelQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<Travel> list = travelMapper.selectForList(qo);
        for (Travel travel : list) {
            StatisVO vo = statisRedisService.getStatisRedis(KeysUtil.TRAVEL_STATIS_VO.join(travel.getId().toString()));
            if (vo != null) {
                travel.setViewnum(vo.getViewnum());
                travel.setReplynum(vo.getReplynum());
                travel.setFavornum(vo.getFavornum());
                travel.setSharenum(vo.getSharenum());
                travel.setThumbsupnum(vo.getThumbsupnum());
            }
        }
        return new PageInfo<>(list);
    }

    @Override
    public Travel getTravelById(Long id) {
        return travelMapper.selectByPrimaryKey(id);
    }

    @Override
    public TravelContent getContent(Long id) {
        return travelContentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Travel> getTravelTop3ByCreatTime(Long destId) {
        return travelMapper.selectTravelTop3ByCreatTime(destId);
    }

    @Override
    public Long saveOrUpdate(Travel travel) {
        //摘要
        String content = travel.getTravelContent().getContent();
        if (content.length() > 200) {
            travel.setSummary(content.substring(0, 200));
        }else {
            travel.setSummary(content);
        }
        //最后更新时间
        travel.setLastUpdateTime(new Date());
        //保存
        if (travel.getId() == null) {
            //创建时间
            travel.setCreateTime(new Date());
            travelMapper.insert(travel);
            TravelContent travelContent = travel.getTravelContent();
            travelContent.setId(travel.getId());
            travelContentMapper.insert(travelContent);
        } else {
            //更新
            travelMapper.updateByPrimaryKey(travel);
            TravelContent travelContent = travel.getTravelContent();
            travelContent.setId(travel.getId());
            travelContentMapper.updateByPrimaryKey(travelContent);
        }

        return travel.getId();
    }

    @Override
    public Travel get(Long id) {
        return travelMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<Travel> queryForListFromBackStage(TravelQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        return new PageInfo<>(travelMapper.selectForListFromBackStage(qo));
    }

    @Override
    public void updateState(Long id, int state) {
        travelMapper.updateState(id, state);
    }

    @Override
    public List<Travel> list() {
        return travelMapper.selectAll();
    }

    @Override
    public void updateRedisStatus(Travel travel) {
        travelMapper.updateRedisStatus(travel);
    }


}
